﻿namespace Jsl.FxCop
{
    using System;
    using Microsoft.VisualStudio.CodeAnalysis.Extensibility;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    [TestClass]
    public class TestTypecastingProblems
    {
        [TestMethod]
        public void Type_GetType_with_a_string_constant_parameter_is_problem()
        {
            new TypecastingProblems()
                .RunMethodRule(
                    () =>
                    {
                        Type type = Type.GetType("System.String");
                    })
                .HasProblem(
                    90,
                    FixCategories.NonBreaking,
                    MessageLevel.Warning,
                    "Use 'typeof(System.String)' instead of 'type.GetType(\"System.String\")'.")
                .HasNoMoreProblems();
        }

        [TestMethod]
        public void Type_GetType_with_a_string_variable_parameter_is_OK()
        {
            new TypecastingProblems()
                .RunMethodRule(
                    () =>
                    {
                        string typeName = "System.String";
                        Type type = Type.GetType(typeName);
                    })
                .HasNoMoreProblems();
        }

        [TestMethod]
        public void Type_GetInterface_with_a_string_constant_parameter_is_problem()
        {
            new TypecastingProblems()
                .RunMethodRule(
                    () =>
                    {
                        Type type = null;
                        type.GetInterface("SomeInterface");
                    })
                .HasProblem(
                    90,
                    FixCategories.NonBreaking,
                    MessageLevel.Warning,
                    "Use '... as SomeInterface' instead of 'type.GetInterface(\"SomeInterface\")'.")
                .HasNoMoreProblems();
        }

        [TestMethod]
        public void Type_GetInterface_with_a_variable_parameter_is_OK()
        {
            new TypecastingProblems()
                .RunMethodRule(
                    () =>
                    {
                        Type type = null;
                        string interfaceName = "SomeInterface";
                        type.GetInterface(interfaceName);
                    })
                .HasNoMoreProblems();
        }
    }
}